package cn.ztydata.teachingsystem.heaven.dao.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 资源浏览量
 *
 * @author fanruiping
 * @since 2014-12-23
 */
@Repository
public class ResourcePVDao {
    protected static Logger logger = LoggerFactory.getLogger(BaseRedisDao.class); //日志记录器

    @Resource(name = "stringRedisTemplate")
    protected RedisTemplate redisTemplate; //sdr-Redis模版

    /**
     * 递增资源浏览量
     * @param resourceId 资源编号
     * @return long
     *
     * @author fanruiping
     * @since 2014-12-23
     */
    public long incrPV(final long resourceId){
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();

        logger.debug("递增资源浏览量");

        return valueOperations.increment(KeyUtils.resourcePV(resourceId),1);
    }

    /**
     * 递增群组资源浏览量(专业群组群组类型=3，群组ID=0)
     *
     * @param groupType 群组类型
     * @param groupId 群组ID
     * @param resourceId 资源ID
     * @return double
     * @author lyj
     * @since 2015-4-1
     */
    public double incrGroupPv(final int groupType, final long groupId, final long resourceId){
        ZSetOperations<String,String> zSetOperations = redisTemplate.opsForZSet();

        logger.debug("递增群组资源浏览量");
        return zSetOperations.incrementScore(KeyUtils.groupRscPv(groupType, groupId), Long.toString(resourceId), 1);
    }

    /**
     * 获取资源浏览量
     * @param resourceId 资源编号
     * @return Integer
     *
     * @author fanruiping
     * @since 2014-12-23
     */
    public Integer getPV(final long resourceId){
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();

        String pv = valueOperations.get(KeyUtils.resourcePV(resourceId));

        if(null == pv){
            logger.debug("pv is null:null == pv");

            valueOperations.set(KeyUtils.resourcePV(resourceId),"0");

            return 0;
        }

        logger.debug("return pv");

        return Integer.parseInt(pv);
    }


    /**
     * 获取群组资源浏览量
     * @param resourceId 资源编号
     * @return Integer
     *
     * @author fanruiping
     * @since 2014-12-23
     */
    public Integer getGroupPV(final int groupType, final long groupId, final long resourceId){
        ZSetOperations<String,String> zSetOperations = redisTemplate.opsForZSet();

        return (int)zSetOperations.score(KeyUtils.groupRscPv(groupType, groupId), Long.toString(resourceId)).doubleValue();
    }


    /**
     * 获取热门资源
     *
     * @param groupType 群组类型
     * @param groupId 群组ID
     * @param count 条数
     * @return Set<Long>
     * @author lyj
     * @since 2015-4-1
     */
    public Set<Long> getOrderByUsedTimes(final int groupType, final long groupId, final int count){
        ZSetOperations<String,Long> zSetOperations = redisTemplate.opsForZSet();

        long min = 0;

        logger.debug("获取热门资源");
        return zSetOperations.reverseRange(KeyUtils.groupRscPv(groupType, groupId), min, count);
    }
}
